// 定义路由配置类型
import { LoaderFunction } from "react-router-dom";

interface GuardedRoute {
  path: string;
  element: React.ReactNode;
  handle?: {
    children?: Array<{ path: string }>;
  };
  meta?: {
    requiresAuth?: boolean;
    roles?: string[];
  };
  loader?: LoaderFunction;
  children?: Array<GuardedRoute>;
}

// 创建路由的函数
export function createRoutes(routes: GuardedRoute[]) {
  return routes.map((route) => {
    const loader = async (args: any) => {
      // 认证检查
      if (route.meta?.requiresAuth) {
        // const user = await getCurrentUser();
        // if (!user) throw redirect("/login");
        // 角色检查
        // if (route.meta?.roles && !route.meta.roles.includes(user.role)) {
        //   throw redirect("/forbidden");
        // }
      }

      // 执行原始loader
      return route.loader ? await route.loader(args) : null;
    };

    return { ...route, loader };
  });
}
